package cn.com.bluemoon.daps.standard.vo;

import cn.com.bluemoon.daps.common.enums.DatabaseType;
import cn.com.bluemoon.daps.common.enums.StandardDataType;
import cn.com.bluemoon.daps.standard.entity.DapStandardBasic;
import com.google.common.collect.Lists;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.beans.BeanUtils;

import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.Arrays;

/**
 * @author Jarod.Kong
 * @date 2020/8/6 11:58
 */
@ApiModel(description = "标准模糊查询")
public class BasicStandardMatchVo implements Serializable {
    private static final long serialVersionUID = 1L;

    @Data
    @Accessors
    public static class InVo {
        @ApiModelProperty(value = "关键字,查询标准中、英文、描述名称")
        @Size(max = 100, message = "关键字不可大于{max}")
        private String keyword;

        @ApiModelProperty(value = "提供模型字段模糊查询中keyword模糊类型，" +
                "模糊查询字段名，模糊查询字段中文名，模糊查询字段描述，若值为null，则模糊查询字段名、字段中文名、字段描述")
        private KeywordType keywordType;

        @ApiModelProperty("数据库类型，1 为mysql，2为pgsql，3为hive")
        private Integer dbType;

        @ApiModelProperty(value = "模糊查询的匹配方式，true左匹配、false为全匹配", example = "true")
        private boolean rightMatch = true;

        /**
         * 转为枚举dbType
         *
         * @return x
         */
        public DatabaseType toDbType() {
            return toDbTypeOrDefault(DatabaseType.MYSQL);
        }

        /**
         * 增加dbType转为db
         *
         * @param dbTypeDefault x
         * @return c
         */
        public DatabaseType toDbTypeOrDefault(DatabaseType dbTypeDefault) {
            if (getDbType() != null) {
                return Arrays.stream(DatabaseType.values())
                        .filter(d -> d.getCode() == getDbType())
                        .findFirst().orElse(dbTypeDefault);
            }
            return dbTypeDefault;
        }

        @SuppressWarnings("rawtypes")
        public BasicStandardFormVo toPageVo() {
            BasicStandardFormVo formVo = new BasicStandardFormVo();
            formVo.setTopicId(null);
            formVo.setCategory1(null);
            formVo.setCategory2(null);
            formVo.setKeyword(getKeyword());
            formVo.setDepartmentId(null);
            formVo.setKeywordType(getKeywordType());
            formVo.setCurrent(0L);
            formVo.setPageSize(0L);
            formVo.setOrders(Lists.newArrayList());
            return formVo;
        }
    }

    /**
     * 输出结果
     */
    @Data
    @Accessors
    public static class OutVo {
        @ApiModelProperty("标准id")
        private String id;

        @ApiModelProperty(value = "业务定义")
        private String businessDefine;

        @ApiModelProperty(value = "数据长度，大于1")
        private String dataLength;

        @ApiModelProperty(value = "数据精度，小数位数多少")
        private String dataScale;

        /**
         * 数据类型
         * 代码类 标志类 文本类 金额类 比例类 数值类 日期类 日期时间类
         * <strong>当{@link DapStandardBasic#getDataType()}为{@link StandardDataType#代码类}</strong>
         * 时，才会有所谓的代码字典{@link cn.com.bluemoon.daps.standard.entity.DapStandardBasicDic},其他不需要存储代码字典
         */
        @ApiModelProperty(value = "数据类型, eg: 0: 代码类,1: 标志类,2: 文本类,3: 金额类,4: 比例类,5: 数值类,6: 日期类,7: 日期时间类,8: 时间类 ")
        private StandardDataType stdDataType;

        @ApiModelProperty("对应数据库的标准数据类型的默认字段类型")
        private String dataType;

        @ApiModelProperty(value = "英文名")
        private String engName;

        @ApiModelProperty(value = "中文名")
        private String name;

        public OutVo(DapStandardBasic standardBasic) {
            if (standardBasic != null) {
                BeanUtils.copyProperties(standardBasic, this);
                // 唯一id
                this.id = standardBasic.getStandardUniqueId();
                this.stdDataType = standardBasic.getDataType();
                this.dataType = null;
            }
        }
    }
}
